#include "../../include/machine/CMachineGroup.h"

CMachineGroup::CMachineGroup()
{
    Clear();
}

CMachineGroup::~CMachineGroup()
{
    Clear();
}

//清除所有数据
void CMachineGroup::Clear()
{
    while(machGroup.size() > 0)
    {
        auto it = machGroup.begin();
        //CMachine *pMach = it->second;
        delete it->second;
        machGroup.erase(it);
    }
}

//从数据库加载机器信息
bool CMachineGroup::LoadMachine(CMySQLOper *pMysql)
{
    if(pMysql == nullptr)
    {
        errMsg = "无效的数据库连接句柄";
        return false;
    }
        
    //查询数据库
    MYSQL_RES *res;
    char sqlQuery[256] = {0x00};
    sprintf(sqlQuery,"select tbl_machine.id,tbl_machine.name,tbl_machine.collector_id,\
        tbl_machine_type.name,tbl_machine_type.property from tbl_machine inner join tbl_machine_type on tbl_machine.type_id = tbl_machine_type.id order by tbl_machine.id asc");
    if((res = pMysql->select(sqlQuery)) == NULL)
    {
        errMsg = pMysql->GetLastErrMsg();
        return false;
    }   
    else
    {                   
        MYSQL_ROW row;
        int s = mysql_num_rows(res);
        while((row = mysql_fetch_row(res)) != NULL)
        {
            CMachine *pMach = new CMachine;
            //机器ID
            if(row[0] != 0x00)
                pMach->machID = atoi(row[0]);
            else
            {
                delete pMach;
                continue;
            }            
            
            //机器名称
            if(row[1] != 0)
                pMach->machName = row[1];
            else
            {
                delete pMach;
                continue;
            }
            
            //采集器ID
            if(row[2] != 0)
                pMach->collectorID = atoi(row[2]);
            else
            {
                delete pMach;
                continue;
            }
            
            //机器类别名称
            if(row[3] != 0)
                pMach->typeName = row[3];
            else
            {
                delete pMach;
                continue;
            }
            
            //属性信息
            if(row[4] != 0)
            {
                std::string strProperty = row[4];
                pMach->property = row[4];
            }
            else
                pMach->property = "";                
            
            machGroup.insert(std::pair<unsigned int,CMachine*>(pMach->machID,pMach));
        }

        mysql_free_result(res);
    }

    return true;
}

//恢复上次关机时设备的数据
bool CMachineGroup::RecoverMachineData(CMySQLOper *pMysql)
{
    if(pMysql == nullptr)
    {
        errMsg = "无效的数据库连接句柄";
        return false;
    }
    
    //查询到的数据
    std::map<unsigned int,CMachineData> mapMachData;

    //查询数据库
    MYSQL_RES *res;
    char sqlQuery[256] = {0x00};
    sprintf(sqlQuery,"select * from tbl_recover order by id asc");
    if((res = pMysql->select(sqlQuery)) == NULL)
    {
        errMsg = pMysql->GetLastErrMsg();
        return false;
    }   
    else
    {                   
        MYSQL_ROW row;
        int s = mysql_num_rows(res);
        while((row = mysql_fetch_row(res)) != NULL)
        {
            CMachineData tempData;
            unsigned int machID;

            //机器ID
            if(row[0] != 0x00)
                machID = atoi(row[0]);
            else   
                continue;              
            
            //机器名称，暂不使用            
            
            //产量
            if(row[2] != 0x00)
                tempData.machOutput = atoi(row[2]);
            else
                continue;
            
            //程序名
            if(row[3] != 0x00)
                tempData.prgName = row[3];
            
            //报警代码
            if(row[4] != 0x00)
                tempData.alarmCode = row[4];

            //报警详情
            if(row[5] != 0x00)
                tempData.alarmMsg = row[5];

            //循环时间
            if(row[6] != 0x00)
                tempData.cycleTime = atof(row[6]);

            //其它记录，暂未使用

            //记录时间戳
            if(row[8] != 0x00)
            {
                
            }
            else
            {
                continue;
            }
                         
            
            mapMachData.insert(std::pair<unsigned int,CMachineData>(machID,tempData));
        }

        mysql_free_result(res);
    }

    //开始匹配
    for(auto &it:machGroup)
    {
        int machID =it.first;
        auto itDst = mapMachData.find(machID);
        if(itDst != mapMachData.end())
            it.second->UpdateMachData(itDst->second);
    }

    return true;
}

//获取设备总数
int CMachineGroup::GetMachineSize(void){ return machGroup.size();}

//获取指定序列号的机器信息
CMachine* CMachineGroup::GetMachineByIndex(unsigned int index)
{
    if(index >= 0 && index < machGroup.size())
    {
        auto it = machGroup.begin();
        for(int i = 0; i<= index;i++)
        {
            if(i == index)
                return it->second;
        
            it++;
        }
    }
    
    return nullptr;
}

//获取指定ID的机器信息
CMachine* CMachineGroup::GetMachineByID(unsigned int id)
{
    auto it = machGroup.find(id);
    if(it != machGroup.end())
        return it->second;

    return nullptr;
}